package com.jj.fly.base;

import java.util.Properties;

import org.apache.commons.logging.Log;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.jj.fly.utils.ConstantUtils;
import com.jj.fly.utils.LogUtils;
import com.jj.fly.utils.PathUtils;

/**
 * 抽象Controller基类
 * 
 * @author JJ
 *
 */
@Controller
public abstract class BaseController extends ServiceController {

	/**
	 * 创建日志对象
	 */
	protected Log logger = LogUtils.getFrameworkLoger();
	/**
	 * 返回封装数据
	 */
	@SuppressWarnings("rawtypes")
	protected DataCenter responseData = new DataCenter();

	// ============== 分页用的参数 =============
	protected int pageNum = 1; // 当前页

	protected int pageSize = 10; // 每页显示多少条记录

	protected Gson gson = new Gson();

	protected String image_server;

	// 使用默认数据源
	public BaseController() {
		MultipleDataSource.setDataSourceKey(ConstantUtils.DATA_SOURCE_WWW);

		Properties properties = PathUtils.getProperties("framework.properties");
		image_server = properties.getProperty("image_server");
	}

	/**
	 * 初始化pagebean
	 * 
	 * @param vo
	 * @return
	 */
	protected PageBean initPageBean(PageBean pageBean) {
		if (pageBean == null)
			pageBean = new PageBean();
		if (pageBean.getCurrentPage() == null)
			pageBean.setCurrentPage(1);
		if (pageBean.getPageSize() == null)
			pageBean.setPageSize(10);
		return pageBean;
	}

	/**
	 * 初始化pagebean
	 * 
	 * @param pageSize
	 * @param currentPage
	 * @return
	 */
	protected PageBean initPageBean(Integer pageSize, Integer currentPage) {
		PageBean pageBean = new PageBean();
		Integer startIndex = null;
		if (currentPage == null) {
			currentPage = 1;
		}
		if (currentPage <= 0) {
			currentPage = 1;
			startIndex = -1;
		} else {
			startIndex = (currentPage - 1) * pageSize;
		}
		pageBean.setCurrentPage(currentPage);
		pageBean.setPageSize(pageSize);
		pageBean.setStartIndex(startIndex);
		return pageBean;
	}

	/**
	 * 批量或单独 更新实体状态
	 * 
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	protected abstract @ResponseBody DataCenter updateStatus(
			@RequestBody BaseQueryVo vo) throws Exception;

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@SuppressWarnings("rawtypes")
	public DataCenter getResponseData() {
		return responseData;
	}

	public void setResponseData(
			@SuppressWarnings("rawtypes") DataCenter responseData) {
		this.responseData = responseData;
	}
}
